import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../providers/provider_container.dart';
import '../intent/home_state.dart';
import '../model/home_viewmodel.dart';

class HomePage extends ConsumerStatefulWidget {
  @override
  ConsumerState<ConsumerStatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends ConsumerState<HomePage> {
  HomeViewModel get viewModel => ref.read(homeViewModelProvider.notifier);

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      viewModel.initData();
    });
  }

  @override
  Widget build(BuildContext context) {
    final HomeState state = ref.watch(homeViewModelProvider);
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        String item = state.functionList[index];
        return InkWell(
          child: Container(
            padding: EdgeInsets.all(12),
            child: Center(child: Text(item)),
          ),
          onTap: () {
            viewModel.goNextPage(index);
          },
        );
      },
      itemCount: state.functionList.length,
    );
  }
}
